package com.cwt_taxi_xa;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cwt_taxi_xa.custom_view.ConditioningView;
import com.cwt_taxi_xa.custom_view.MyToggleView;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.can.ConditioningDecode;
import com.cwt_taxi_xa.protocol.can.IViewAirCondition;
import com.cwt_taxi_xa.protocol.can.manager.ViewManager;
import com.cwt_taxi_xa.util.AppLog;


public class ActivityConditioning extends CWTBaseActivity implements IViewAirCondition,View.OnClickListener{

	private ConditioningView  cvWindLevel;
	private ImageView ivWindToFace,ivWindToFront,ivWindToKnee;
	private String TAG = "ActivityConditioning";
	private TextView tvWindGear,tvTemperature,tvTemperatureUnit;
	private ImageButton ibtPower;
	private MyToggleView ibtPtcHeater,ibtFuelHeater;
	private LinearLayout llHeaterToggle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.openSwipeBack(false);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conditioning);

		cvWindLevel =  (ConditioningView) findViewById(R.id.cw_wind_level);
		ivWindToFace = (ImageView) findViewById(R.id.iv_wind_to_face);
		ivWindToFront = (ImageView) findViewById(R.id.iv_wind_to_front);
		ivWindToKnee = (ImageView) findViewById(R.id.iv_wind_to_knee);
		tvWindGear = (TextView)findViewById(R.id.tv_wind_gear);
		tvTemperature = (TextView)findViewById(R.id.tv_temperature);
		tvTemperatureUnit = (TextView)findViewById(R.id.tv_temperature_unit);
		ibtPower = (ImageButton)findViewById(R.id.ibt_conditioned_power);
		ibtPtcHeater = (MyToggleView)findViewById(R.id.ibt_ptc_heater);
		ibtFuelHeater = (MyToggleView)findViewById(R.id.ibt_fuel_heater);
		llHeaterToggle = (LinearLayout)findViewById(R.id.ll_heater_toggle);
		findViewById(R.id.btn_conditioning_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		ibtPtcHeater.setOnClickListener(this);
		ibtFuelHeater.setOnClickListener(this);



	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//背光
		ViewManager.getInstance(this).hiddenFloatBall();
		MCUUtil.mConditioningDecode.register(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//背光
		MCUUtil.mConditioningDecode.unregister(this);
		ViewManager.getInstance(this).showFloatBall();
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}

	@Override
	public void heaterType(int heaterType) {
		//显示可切换
		if(heaterType == 3){//PTC+燃油加热器
			ibtPtcHeater.setVisibility(View.VISIBLE);
			ibtFuelHeater.setVisibility(View.VISIBLE);
			ibtPtcHeater.setClickable(true);
			ibtFuelHeater.setClickable(true);
            llHeaterToggle.setVisibility(View.VISIBLE);
            llHeaterToggle.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
		}else if(heaterType == 2){//燃油加热器
			ibtPtcHeater.setVisibility(View.GONE);
			ibtFuelHeater.setVisibility(View.VISIBLE);
			ibtFuelHeater.setClickable(false);
			llHeaterToggle.setVisibility(View.VISIBLE);
            llHeaterToggle.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
		}else if(heaterType == 1){//PTC 加热
            ibtFuelHeater.setVisibility(View.GONE);
            ibtPtcHeater.setVisibility(View.VISIBLE);
			ibtPtcHeater.setClickable(false);
			llHeaterToggle.setVisibility(View.VISIBLE);
			llHeaterToggle.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
		}else if(heaterType == 0){
			ibtPtcHeater.setVisibility(View.GONE);
			ibtFuelHeater.setVisibility(View.GONE);
			llHeaterToggle.setVisibility(View.GONE);
		}else if(heaterType >= 4){
			ibtPtcHeater.setVisibility(View.GONE);
			ibtFuelHeater.setVisibility(View.GONE);
			llHeaterToggle.setVisibility(View.GONE);
		}
	}

	@Override
	public void heatingMode(int heatingMode) {
		//0x00：无效
		//0x01：PTC 加热模式
		//0x02：燃油加热器模式
		//0x03：预留
		//当前模式
		AppLog.d(TAG,"heatingMode:" + heatingMode);
		if(heatingMode == 2){
			ibtPtcHeater.setSrc(R.drawable.ic_heater_type_ptc);
			ibtFuelHeater.setSrc(R.drawable.ic_heater_type_fuel_press);
		}else if(heatingMode == 1){
			ibtPtcHeater.setSrc(R.drawable.ic_heater_type_ptc_press);
			ibtFuelHeater.setSrc(R.drawable.ic_heater_type_fuel);
		}else{

		}
	}

	@Override
	public void windLevel(int level) {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"windLevel:" + level);
		if(cvWindLevel != null && level < 8){
			cvWindLevel.selectLevel(level);
			tvWindGear.setText(level +"");
		}else{
			AppLog.e(TAG,"2 windLevel:" + level);
			tvWindGear.setText("" + level);

		}

	}

	@Override
	public void outletMode(int type) {
		// TODO Auto-generated method stub

		if(type == 0x00){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_off);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_off);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_off);
		}else if(type == 0x01){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_on);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_off);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_off);
		}else if(type == 0x02){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_on);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_off);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_on);
		}else if(type == 0x03){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_off);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_off);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_on);
		}else if(type == 0x04){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_off);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_on);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_on);
		}else if(type == 0x05){
			ivWindToFace.setImageResource(R.drawable.ic_wind_to_face_off);
			ivWindToFront.setImageResource(R.drawable.ic_wind_to_front_on);
			ivWindToKnee.setImageResource(R.drawable.ic_wind_to_knee_off);
		}else{
			AppLog.e(TAG,"outletMode type:" + type);
		}
	}

	@Override
	public void showTemperature(String temp) {
		tvTemperature.setText(temp);
	}

	@Override
	public void showTemperatureUnit(String unit) {
		tvTemperatureUnit.setText(unit);
		tvTemperatureUnit.setVisibility(View.VISIBLE);
	}

	@Override
	public void errorCode(int code) {
		// TODO Auto-generated method stub
		if(1 == code){
			tvTemperatureUnit.setVisibility(View.VISIBLE);
			AppLog.d(TAG,"errorCode 不显示!");
		}if (2 == code){//显示错误码
			tvTemperatureUnit.setVisibility(View.INVISIBLE);
			AppLog.d(TAG,"errorCode 显示!");
		}else{
			AppLog.d(TAG,"errorCode exit!");
		}
	}

	@Override
	public void power(int power) {
		// TODO Auto-generated method stub
		Log.d(TAG,"power():power" + power);
		if(power == 0){
			//0x0:无效;
			Log.d(TAG,"空调开关:无效");
			//多功能屏显示 保持上一状态。
		}else if(power == 1){
			//0x1:打开;
			ibtPower.setImageResource(R.drawable.turn_on_air_conditioned);
			Log.d(TAG,"空调开关:打开");
		}else if(power == 2){
			//0x2:关闭;
			Log.d(TAG,"空调开关:关闭");

			ibtPower.setImageResource(R.drawable.turn_off_air_conditioned);
			//不显示温度值、单位℃（℉）及“温度”
			//连续5秒没有采集到0x13B的报文时，不显示温度值、单位℃（℉）及“温度”
		}else if(power == 3){
			//0x3:保留;
			Log.d(TAG,"空调开关:保留");
			//多功能屏显示 保持上一状态。
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ibt_ptc_heater:
				ConditioningDecode.setPtcHeater();
				break;
			case R.id.ibt_fuel_heater:
				ConditioningDecode.setFuelHeater();
				break;
		}
	}
}
